package com.example.yunhailutong.zhongtan.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.yunhailutong.zhongtan.R;
import com.example.yunhailutong.zhongtan.activity.ActivityBase;
import com.example.yunhailutong.zhongtan.activity.ActivityGongYingDesc;
import com.example.yunhailutong.zhongtan.activity.ActivityXuQiuDesc;
import com.example.yunhailutong.zhongtan.app.OldsEvent;
import com.example.yunhailutong.zhongtan.entity.XuQiuEntity;
import com.example.yunhailutong.zhongtan.net.PersionBiz;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.squareup.picasso.Picasso;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by yunhailutong on 2018/4/26.
 */

@SuppressLint("ValidFragment")
public class FragmentXuQiu extends BaseFragment {
    Unbinder unbinder;
    @BindView(R.id.listview)
    ListView listview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private View view;
    private String id = "";

    public FragmentXuQiu(String id) {
        this.id = id;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        view = inflater.inflate(R.layout.fragment_baselistview, container, false);
        // TODO: add setContentView(...) invocation
        unbinder=ButterKnife.bind(this, view);
        if(!EventBus.getDefault().isRegistered(this)){//加上判断
            EventBus.getDefault().register(this);
        }
        Map<String, String> params = new HashMap<>();
        params.put("index", "1");
        params.put("length","100");
        params.put("areaCode",id);

        new PersionBiz().GetXuQiuList(params);

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                Map<String, String> params = new HashMap<>();
                params.put("index", "1");
                params.put("length","100");
                params.put("areaCode",id);

                new PersionBiz().GetXuQiuList(params);
                //传入false表示刷新失败
                refreshlayout.finishRefresh();
            }
        });
        return view;
    }

    //Model and View connect
    private void initListView(final XuQiuEntity entity) {
        listview.setAdapter(new CommonAdapter<XuQiuEntity.DataBean>(getActivity(), R.layout.main_item_base, entity.getData()) {
            @Override
            protected void convert(ViewHolder viewHolder, XuQiuEntity.DataBean item, int position) {
                viewHolder.setText(R.id.title, item.getTitle());
                viewHolder.setText(R.id.text1, item.getPrice());
                viewHolder.setText(R.id.text2, item.getNum()+"吨");
                Picasso.with(mContext).load("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3094121875,3546519224&fm=27&gp=0.jpg").into((ImageView) viewHolder.getView(R.id.image));
                if(!item.getImg().equals("")){
                    String image = item.getImg();
                    String encode = null;
                    try {
                        encode = URLEncoder.encode(image, "UTF-8");
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    String replace = encode.replace("%3A", ":").replace("%2F", "/");
                    Picasso.with(mContext).load(replace).into((ImageView) viewHolder.getView(R.id.image));
                }
            }
        });

        //设置点击事件
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), ActivityXuQiuDesc.class);
                intent.putExtra("id",entity.getData().get(position).getId());
                startActivity(intent);
            }
        });
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
    @Subscribe
    public void log(OldsEvent.XuQiuEvent msg) {
        if (msg.what == -1) {
            disPlay(msg.obj.toString());
        }
        if (msg.what == 1) {
            initListView((XuQiuEntity) msg.obj);
        }
    }

    @Override
    public void onClick(View v) {

    }
}
